package com.cx.common.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author flysand
 * @package com.cx.common.util
 * @date 2021-11-08 22:19
 * @des StringUtils:
 */
public class StringUtils {

    /**
     * 是否为空白  包含空格字符串
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(final CharSequence cs) {
        final int strLen = length(cs);
        if (strLen == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static int length(final CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

    /**
     * 是否为空 null 或 空字符串""
     *
     * @param cs
     * @return
     */
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    /**
     * 字符串转byte[]
     *
     * @param string
     * @return
     */
    public static byte[] getBytesUtf8(String string) {
        return getBytes(string, StandardCharsets.UTF_8);
    }

    private static byte[] getBytes(String string, Charset charset) {
        return string == null ? null : string.getBytes(charset);
    }

    /**
     * byte[] 转 String
     *
     * @param b
     * @return
     */
    public static String getStringUtf8(byte[] b) {
        return new String(b, StandardCharsets.UTF_8);
    }
}
